package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.example.common.R;
import org.example.common.StatusCode;
import org.example.pojo.entity.SysDept;
import org.example.pojo.req.DeptReq;
import org.example.pojo.vo.DeptVo;
import org.example.service.SysDeptService;
import org.example.mapper.SysDeptMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 翟旦旦
 * @description 针对表【sys_dept(部门表)】的数据库操作Service实现
 * @createDate 2024-05-06 20:44:43
 */
@Service
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept>
        implements SysDeptService {


    /**
     * 获取部门tree
     *
     * @param deptName
     * @param status
     * @return
     */
    @Override
    public List<DeptVo> tree(String deptName, String status) {
        List<SysDept> sysDepts = this.list();
        //构造菜单树
        List<DeptVo> deptVoList = new ArrayList<>();
        LambdaQueryWrapper<SysDept> wrapper = new LambdaQueryWrapper();

        if (StringUtils.isNoneBlank(status) && StringUtils.isNotBlank(deptName)) {
            wrapper.like(SysDept::getDeptName, deptName);
            wrapper.eq(SysDept::getStatus, status);
            SysDept sysDept = this.getOne(wrapper);
            //构建菜单树
            listMenuTreeCondition(deptVoList, sysDepts, sysDept);
            return deptVoList;
        }

        if (StringUtils.isNoneBlank(deptName)) {
            wrapper.like(SysDept::getDeptName, deptName);
            SysDept sysDept = this.getOne(wrapper);
            if (sysDept!=null){
                //构建菜单树
                listMenuTreeCondition(deptVoList, sysDepts, sysDept);
            }
            return deptVoList;
        }
        if (StringUtils.isNoneBlank(status)) {
            wrapper.eq(SysDept::getStatus, status);
            List<SysDept> sysDept = this.list(wrapper);
            //构建菜单树
            listMenuTree(deptVoList, sysDept);
            return deptVoList;
        }

        //无条件查询默认显示所有
        listMenuTree(deptVoList, sysDepts);
        return deptVoList;
    }

    /**
     * 构建部门菜单树
     *
     * @param deptVoList
     * @param sysDepts
     */
    private void listMenuTree(List<DeptVo> deptVoList, List<SysDept> sysDepts) {

        for (SysDept sysDept : sysDepts) {
            if (sysDept.getParentId().toString().equals("0")) {
                DeptVo deptVo = new DeptVo();
                BeanUtils.copyProperties(sysDept, deptVo);
                deptVo.setChildren(findChildren(sysDepts, sysDept));
                deptVoList.add(deptVo);
            }
        }
    }

    /**
     * 构建部门菜单树
     *
     * @param deptVoList
     * @param sysDepts
     * @param dept
     */
    private void listMenuTreeCondition(List<DeptVo> deptVoList, List<SysDept> sysDepts, SysDept dept) {
        for (SysDept sysDept : sysDepts) {
            if (sysDept.getParentId().toString().equals("0") && sysDept.getDeptName().equals(dept.getDeptName())) {
                DeptVo deptVo = new DeptVo();
                BeanUtils.copyProperties(sysDept, deptVo);
                deptVo.setChildren(findChildren(sysDepts, sysDept));
                deptVoList.add(deptVo);
            }
        }
    }

    /**
     * 递归查询部门子级数据
     *
     * @param sysDepts
     * @param sysDept
     * @return
     */
    private List<DeptVo> findChildren(List<SysDept> sysDepts, SysDept sysDept) {
        List<DeptVo> deptVos = new ArrayList<>();
        sysDepts.forEach(item -> {
            if (item.getParentId().equals(sysDept.getDeptId())) {
                DeptVo deptVo = new DeptVo();
                BeanUtils.copyProperties(item, deptVo);
                deptVo.setChildren(findChildren(sysDepts, item));
                deptVos.add(deptVo);
            }
        });
        return deptVos;
    }


    /**
     * 修改部门信息
     *
     * @param dept
     */
    @Override
    public void editDept(SysDept dept) {
        //修改父级菜单数据
        SysDept sysDept = this.getById(dept.getDeptId());
        SysDept parentDept = this.getById(sysDept.getParentId());
        parentDept.setDeptName(dept.getDeptName());
        this.updateById(parentDept);
        //修改当前菜单数据
        this.updateById(dept);
    }

    /**
     * 删除部门信息
     *
     * @param id
     */
    @Override
    public R removeDept(String id) {
        LambdaQueryWrapper<SysDept> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysDept::getParentId, id);
        List<SysDept> depts = this.list(wrapper);
        if (!CollectionUtils.isEmpty(depts)) {
            return R.ok(StatusCode.MENU_EXIT_CHILDREN.name());
        }
        //删除部门
        this.removeById(id);
        return R.ok("删除成功");
    }

    /**
     * 添加编辑部门信息
     *
     * @param deptReq
     */
    @Override
    public void addOrDept(DeptReq deptReq) {
        SysDept sysDept = new SysDept();
        BeanUtils.copyProperties(deptReq, sysDept);
        sysDept.setDeptName(deptReq.getLabel());
        this.saveOrUpdate(sysDept);
    }
}




